package com.quanjiaolian.userinfo.serviceimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quanjiaolian.DB.DataBase;
import com.quanjiaolian.userinfo.service.OrderInfoService;

public class OrderInfoServiceImpl implements OrderInfoService {
	
	DataBase db = new DataBase();

	/**
	 * 声明数据库连接实例
	 */
	// 定义一个Connection 用来连接数据库
	Connection conn = null;
	// 声明PreparedStatement实例
	PreparedStatement pstmt = null;
	// 用于存放返回结果的数据集
	ResultSet rs = null;
	
	@SuppressWarnings("static-access")
	public List<Object> queryMyOrder(int user_id){
		List<Object> list = new ArrayList<Object>();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT oi.order_id,ii.inspic_path,ii.ins_name,oi.order_date ");
		sql.append("FROM order_info oi,ins_info ii ");
		sql.append("WHERE oi.ins_id = ii.ins_id AND oi.user_id = ?");
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1,user_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("order_id",rs.getString(1));
				map.put("inspic_path",rs.getString(2));
				map.put("ins_name",rs.getString(3));
				map.put("order_date",rs.getString(4));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@SuppressWarnings("static-access")
	public List<Object> queryOrderMy(int ins_id){
		List<Object> list = new ArrayList<Object>();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT oi.order_id,ui.userpic_path,ui.username,oi.order_date ");
		sql.append("FROM order_info oi,user_info ui ");
		sql.append("WHERE oi.ins_id = ui.userid AND oi.ins_id = ? AND oi.order_state = 0");
		System.out.println("queryOrderMy sql is: "+sql.toString());
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1,ins_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("order_id",rs.getString(1));
				map.put("userpic_path",rs.getString(2));
				map.put("user_name",rs.getString(3));
				map.put("order_date",rs.getString(4));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@SuppressWarnings("static-access")
	public Map<String,Object> queryUserOrderDetail(String order_id){
		Map<String,Object> map = new HashMap<String,Object>();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ii.ins_id,ii.ins_name,ii.ins_msisdn,ii.inspic_path,oi.order_id,oi.order_date,oi.order_pay,oi.teach_way,oi.test_date,oi.test_addr,oi.order_state ");
		sql.append("FROM order_info oi,ins_info ii ");
		sql.append("WHERE oi.ins_id = ii.ins_id AND oi.order_id = ?");
		conn = db.getconn();
		try {
			System.out.println("queryOrderMy sql is :"+sql.toString());
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1,order_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				map.put("ins_id",rs.getInt(1));
				map.put("ins_name",rs.getString(2));
				map.put("ins_msisdn",rs.getString(3));
				map.put("inspic_path",rs.getString(4));
				map.put("order_id",rs.getString(5));
				map.put("order_date",rs.getString(6));
				map.put("order_pay",rs.getString(7));
				map.put("teach_way",rs.getString(8));
				map.put("test_date",rs.getString(9));
				map.put("test_addr",rs.getString(10));
				map.put("order_state",rs.getInt(11));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	@SuppressWarnings("static-access")
	public Map<String,Object> queryInsOrderDetail(String order_id){
		Map<String,Object> map = new HashMap<String,Object>();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ui.userpic_path,ui.username,oi.order_date,oi.order_pay,oi.order_id,oi.teach_way,oi.test_date,oi.test_addr,oi.order_state ");
		sql.append("FROM order_info oi,user_info ui ");
		sql.append("WHERE oi.user_id = ui.userid AND oi.order_id = ?");
		conn = db.getconn();
		try {
			System.out.println("queryInsOrderDetail sql is :"+sql.toString());
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1,order_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				map.put("userpic_path",rs.getString(1));
				map.put("user_name",rs.getString(2));
				map.put("order_date",rs.getString(3));
				map.put("order_pay",rs.getString(4));
				map.put("order_id",rs.getString(5));
				map.put("teach_way",rs.getString(6));
				map.put("test_date",rs.getString(7));
				map.put("test_addr",rs.getString(8));
				map.put("order_state",rs.getInt(9));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return map;
	}

}
